// VoterSurveyIdeology
log using VoterSurveyIdeology.log, replace
use "C:\Users\sbstjp\OneDrive - Cardiff University\voter_panel.dta" // Democracy Fund Voter Study Group. VIEWS OF THE ELECTORATE RESEARCH SURVEY. Washington, D.C. https://www.voterstudygroup.org/.  Date accessed: March 09, 2025.

// Create social justice scale
*Delete missing
replace ft_blm_2020Sep=. if ft_blm_2020Sep>100
replace reparations_2020Sep=. if reparations_2020Sep>2
replace defundpolice_2020Sep=. if defundpolice_2020Sep>2 
replace police_threat_2020Sep=. if police_threat_2020Sep>2
replace usa_founders_2020Sep=. if usa_founders_2020Sep>2
replace internetharass_dem_2020Sep=. if internetharass_dem_2020Sep==9

*Reverse coding so social justice values are coded high
foreach var in reparations_2020Sep defundpolice_2020Sep usa_founders_2020Sep {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in ft_blm_2020Sep rreparations_2020Sep rdefundpolice_2020Sep rusa_founders_2020Sep police_threat_2020Sep internetharass_dem_2020Sep {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

*At this stage, the scale has a Cronbach's alpha of 0.83

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values
foreach var in sft_blm_2020Sep srreparations_2020Sep srdefundpolice_2020Sep srusa_founders_2020Sep spolice_threat_2020Sep sinternetharass_dem_2020Sep {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreSJV = 0
gen count_nonzeroSJV = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in sft_blm_2020Sep srreparations_2020Sep srdefundpolice_2020Sep srusa_founders_2020Sep spolice_threat_2020Sep sinternetharass_dem_2020Sep {
    replace total_scoreSJV = total_scoreSJV + `var'
    replace count_nonzeroSJV = count_nonzeroSJV + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen SocJusValues = .
replace SocJusValues = total_scoreSJV / count_nonzeroSJV if count_nonzeroSJV > 0

// Liberalism scale
*Delete missing values
replace immi_contribution_2020Nov=. if immi_contribution_2020Nov==4 
replace immi_muslim_2020Nov=. if immi_muslim_2020Nov==5 
replace view_abortlegal_2020Nov=. if view_abortlegal_2020Nov==4 
replace view_gaymar_2020Nov=. if view_gaymar_2020Nov==3 


*Reverse coding so liberal values are coded high
foreach var in immi_contribution_2020Nov view_abortlegal_2020Nov view_gaymar_2020Nov rm1_enriched_2020Nov {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in rrm1_enriched_2020Nov rview_gaymar_2020Nov rview_abortlegal_2020Nov rimmi_contribution_2020Nov sexism_complain_2020Nov sexism_equality_2020Nov immi_muslim_2020Nov {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}


* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values
foreach var in srrm1_enriched_2020Nov srview_gaymar_2020Nov srview_abortlegal_2020Nov srimmi_contribution_2020Nov ssexism_complain_2020Nov ssexism_equality_2020Nov simmi_muslim_2020Nov {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreAL = 0
gen count_nonzeroAL = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in srrm1_enriched_2020Nov srview_gaymar_2020Nov srview_abortlegal_2020Nov srimmi_contribution_2020Nov ssexism_complain_2020Nov ssexism_equality_2020Nov simmi_muslim_2020Nov {
    replace total_scoreAL = total_scoreAL + `var'
    replace count_nonzeroAL = count_nonzeroAL + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen LibValues = .
replace LibValues = total_scoreAL / count_nonzeroAL if count_nonzeroAL > 0

// Self-reported ideology and Democratic ID
replace ideo5_2020Sep=. if ideo5_2020Sep==6 // not sure, 6%

egen maxval = max(ideo5_2020Sep)
gen Rideo5_2020Sep = maxval + 1 - ideo5_2020Sep
drop maxval

replace ft_dem_2020Sep=. if inlist(ft_dem_2020Sep, 997, 998) // don't know, 3%

// Correlations
pwcorr SocJusValues LibValues ft_dem_2020Sep Rideo5_2020Sep [aweight=weight_allpanel_2020Sep], sig 

// Ideological constraint
*Create graduate dummy
gen Graduate=.
replace Graduate=1 if inlist(educ_2020Sep, 5, 6)
replace Graduate=0 if inrange(educ_2020Sep, 1, 4)

// Correlations
pwcorr ft_blm_2020Sep Rreparations_2020Sep Rdefundpolice_2020Sep Rusa_founders_2020Sep police_threat_2020Sep [aweight=weight_genpop_2020Sep], sig
pwcorr ft_blm_2020Sep Rreparations_2020Sep Rdefundpolice_2020Sep Rusa_founders_2020Sep police_threat_2020Sep if newsint_2020Sep==1 [aweight=weight_genpop_2020Sep], sig 
pwcorr ft_blm_2020Sep Rreparations_2020Sep Rdefundpolice_2020Sep Rusa_founders_2020Sep police_threat_2020Sep if Graduate==1 [aweight=weight_genpop_2020Sep], sig 
pwcorr ft_blm_2020Sep Rreparations_2020Sep Rdefundpolice_2020Sep Rusa_founders_2020Sep police_threat_2020Sep if ideo5_2020Sep==1 [aweight=weight_genpop_2020Sep], sig 
pwcorr ft_blm_2020Sep Rreparations_2020Sep Rdefundpolice_2020Sep Rusa_founders_2020Sep police_threat_2020Sep if pid7_2020Sep==1 [aweight=weight_genpop_2020Sep], sig 

log close